package cn.com.customer.manager.wigeit.floatview.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;



import java.util.Objects;

import cn.com.customer.manager.R;
import cn.com.customer.manager.bean.CustomerInfo;
import cn.com.customer.manager.wigeit.floatview.AppSwitchView;

/**
 */
public class AppMonitorService extends Service {

    private AppSwitchView mAppSwitchView;
    public static final String CHANNEL_ID = "AppMonitorService";
    /**
     * 显示弹窗
     *
     * @param info
     */
    public static void start(Context context, CustomerInfo info) {
        Intent intent = new Intent(context, AppMonitorService.class);
        Bundle bundle=new Bundle();
//        bundle.putParcelable("info",info);
        intent.putExtra("bundle",bundle);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
        Log.e("phone","弹窗服务注册完成");
    }

    /**
     * 关闭弹窗
     *
     * @param context
     */
    public static void stop(Context context) {
        context.stopService(new Intent(context, AppMonitorService.class));
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("phone","已传到弹窗");
        if (intent != null) {
           Bundle bundle=intent.getBundleExtra("bundle");
            init(bundle.getParcelable("info"));
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onCreate() {
        super.onCreate();
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "应用监测", NotificationManager.IMPORTANCE_HIGH);
            Objects.requireNonNull(manager).createNotificationChannel(channel);
            builder = new Notification.Builder(this, CHANNEL_ID);
        } else {
            builder = new Notification.Builder(this);
        }
        startForeground(1000, builder.setContentTitle("应用监测")
                .setContentText("正在监测手机的应用使用情况")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(false)
                .setOngoing(true)
                .build());
    }

    private void init(CustomerInfo info) {
        if (mAppSwitchView == null) {
            mAppSwitchView = new AppSwitchView(this);
            mAppSwitchView.show();
            Log.e("phone","弹窗已显示");
        }
        mAppSwitchView.updateAppInfo(info);

    }

    @Override
    public void onDestroy() {
        if (mAppSwitchView != null) {
            mAppSwitchView.clear();
            mAppSwitchView = null;
        }
        super.onDestroy();
    }
}
